var quizApp = angular.module("quizApp", ['ngAnimate', 'ngResource']);

quizApp.config(['$resourceProvider', '$logProvider', function($resourceProvider, $logProvider){
    $resourceProvider.defaults.stripTrailingSlashes = false;
    $logProvider.debugEnabled(true);
}]);

quizApp.controller('foxController', function($scope, $resource, $log){
    var q = 0;
	// ten obiekt trzeba przystosować do struktury, żeby nie wyrzucał błędów
	$scope.quiz = {
        "_id": "0",
        "__v": 1,
        "question":[{
            "_id" : "0",
            "quiz": 0,
            "content": "Jak masz na imię?",
            "answer": [{}]
        }]
    };

	// tablica z odpowiedziami
    $scope.answers = [];
    // aktualnie wybrana odpowiedz
    $scope.answerId = Infinity; // Infinity tylko dlatego ze chcialem jakos zainicializowac ;)
    // tablica wybranych odpowiedzi
    $scope.selectedAnswers = [];
    // czy pusty
    $scope.nonEmpty = false;
    $scope.s = 0;
    $scope.nope = false;

    $scope.tmp = undefined;

    // cała do poprawke,
    $scope.init = function() {
        var quiz = $resource('http://localhost:3000/q/548e0c81fe0d41941bcf35b4',
            { callback: "JSON_CALLBACK" },
            { get: { method: "JSONP" }}
        );

        quiz.get()
            .success( function( data ){
                $scope.tmp = "tak";
            })
            .error( function(){
                $scope.tmp = "nie";
            })

	};

    this.getTmp = function(){
      return $scope.tmp;
    };

    this.getQuestion = function () {
        //return $scope.questions.quizQuestions[q].question;
    };

    this.next = function(){
        //if( q == $scope.questions.quizQuestions.length - 1 ) return;
        q += 1;
        this.setAnswer(q);
    };

    this.prev = function(){
        if( q == 0 ) return;
        q -= 1;
        this.setAnswer(q);
    };
	
	this.currentQuestion = function() {
		return q;
	};
	
	this.questionLastIndexOf = function() {
		//return $scope.questions.quizQuestions.length - 1;
	};

    this.getAnswers = function(){
        //return $scope.questions.quizQuestions[q].answers || [];
    };

    this.selectAnswer = function ( answerId ){
        this.saveAnswer( q, answerId );
        this.next();
    };

    this.isSelected = function ( answerId ){
        return $scope.selectedAnswers[q] === answerId;
    };

    // zapisywanie dopoweidzi
    this.saveAnswer = function( questionId, answerId ){
        if ( $scope.selectedAnswers[questionId] == null ) {
            $scope.selectedAnswers[questionId] = answerId;
            $scope.s += 1;
        } else if ( $scope.selectedAnswers[questionId] != null ){
            $scope.selectedAnswers[questionId] = answerId;
        }
    };

    // ustaawianie wybranej odpowiedzi
    this.setAnswer = function( questionId ){
        if ( $scope.selectedAnswers[questionId] == 'undefined' ) {
            this.isSelected( $scope.selectedAnswers[questionId] );
        }
    };

    //
    this.allAnswers = function () {
        return $scope.s == this.questionLastIndexOf();
    };
    this.itsOK = function () {
        return $scope.nope;
    }
    this.check = function(){
        $scope.nope = true;
    }
});

